package com.besttone.action;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.solr.common.SolrDocumentList;

import com.besttone.bean.ReceiveCouponBean;
import com.besttone.enums.EnumAPIResultCode;
import com.besttone.util.ReceiveCoupon2SolrUtil;
import com.besttone.util.SolrObject;
import com.jfinal.core.Controller;

/**
 * 测试案例：http://localhost:8080/sms_2_solr/receiveCoupon/queryReceiveCoupon...
 * 
 * @author Administrator
 * 
 */
public class ReceiveCouponAction extends Controller {
    private static Log _log = LogFactory.getLog(ReceiveCouponAction.class);

    @SuppressWarnings("rawtypes")
    public void queryReceiveCoupon() {
	String streaming_no = getPara("streaming_no");
	String coupon_code = getPara("coupon_code");
	String phone = getPara("phone");
	String create_time_from = getPara("create_time_from");
	String create_time_to = getPara("create_time_to");
	String current_page = StringUtils.isBlank(getPara("current_page")) ? "1"
		: getPara("current_page");// 当前页数
	String per_page_size = StringUtils.isBlank(getPara("per_page_size")) ? "10"
		: getPara("per_page_size");// 每页条数，默认10
	_log.info("请求查询参数:streaming_no=" + streaming_no + ",coupon_code="
		+ coupon_code + ",phone=" + phone + ",create_time_from="
		+ create_time_from + ",create_time_to=" + create_time_to);
	if (StringUtils.isBlank(create_time_from)) {
	    setAttr("result_code",
		    EnumAPIResultCode.PARAMS_INCOMPLETE.getValue());
	    setAttr("result_msg", "查询开始时间不能为空");
	    renderJson();
	    return;
	}
	StringBuffer strBuf = new StringBuffer();
	// 类型是string,只能精确到天；如果需要精确到秒，需要solr端修改成date类型
	if (StringUtils.isNotBlank(create_time_from)) {
	    create_time_from = StringUtils.trim(create_time_from);
	    strBuf.append(" (create_time:[");
	    strBuf.append(create_time_from);
	    strBuf.append(" TO ");
	    if (StringUtils.isNotBlank(create_time_to)) {
		strBuf.append(StringUtils.trim(create_time_to));
	    } else {
		strBuf.append("*");
	    }
	    strBuf.append("]) ");
	    if (StringUtils.isNotBlank(create_time_to)) {
		create_time_to = StringUtils.trim(create_time_to);
		strBuf.append(" AND (create_time:[* TO ");
		strBuf.append(StringUtils.trim(create_time_to));
		strBuf.append("]) ");
	    }
	    if (StringUtils.isNotBlank(coupon_code)) {
		coupon_code = StringUtils.trim(coupon_code);
		strBuf.append(" AND (coupon_code:");
		strBuf.append(coupon_code);
		strBuf.append(") ");
	    }
	    if (StringUtils.isNotBlank(phone)) {
		phone = StringUtils.trim(phone);
		strBuf.append(" AND (phone:");
		strBuf.append(phone);
		strBuf.append(") ");
	    }
	}
	String querySql = strBuf.toString();
	_log.info("solr查询语句:" + querySql);
	try {
	    setAttr("streaming_no", streaming_no);
	    SolrDocumentList docList = ReceiveCoupon2SolrUtil.search(querySql,
		    Integer.valueOf(current_page),
		    Integer.valueOf(per_page_size));
	    setAttr("result_code", EnumAPIResultCode.SUCCESS.getValue());
	    List list = SolrObject.toBeanList(docList, ReceiveCouponBean.class);
	    setAttr("result_msg", list);// 返回详情数据
	    setAttr("result_num", docList.getNumFound());
	} catch (Exception e) {
	    _log.info("solr查询异常:" + e);
	    setAttr("result_code", EnumAPIResultCode.SYSTEM_ERROR.getValue());
	    setAttr("result_msg", EnumAPIResultCode.SYSTEM_ERROR.getName());
	}
	renderJson();
	return;
    }
}
